/// 对话列表组件
/// 显示当前用户的所有聊天对话
library;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:chat_project/providers/chat_provider.dart';
import 'package:chat_project/widgets/conversation_list_item.dart';
import 'package:go_router/go_router.dart';

class ConversationList extends StatelessWidget {
  const ConversationList({super.key});

  @override
  Widget build(BuildContext context) {
    final chatProvider = Provider.of<ChatProvider>(context);
    final conversations = chatProvider.conversations;
    final isLoading = chatProvider.isLoading;
    final error = chatProvider.error;
    final selectedConversationId = chatProvider.selectedConversationId;

    if (isLoading) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }

    if (error != null) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(
              Icons.error_outline,
              color: Colors.red,
              size: 48,
            ),
            const SizedBox(height: 16),
            Text(
              '加载失败',
              style: Theme.of(context).textTheme.titleMedium,
            ),
            const SizedBox(height: 8),
            Text(
              error,
              style: Theme.of(context).textTheme.bodyMedium,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () => chatProvider.loadConversations(),
              child: const Text('重试'),
            ),
          ],
        ),
      );
    }

    if (conversations.isEmpty) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.chat_bubble_outline,
              color: Colors.grey[400],
              size: 64,
            ),
            const SizedBox(height: 16),
            Text(
              '暂无聊天',
              style: Theme.of(context).textTheme.titleMedium?.copyWith(
                    color: Colors.grey[600],
                  ),
            ),
            const SizedBox(height: 8),
            Text(
              '点击右上角的加号开始新的对话',
              style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                    color: Colors.grey[500],
                  ),
            ),
          ],
        ),
      );
    }

    return ListView.builder(
      itemCount: conversations.length,
      itemBuilder: (context, index) {
        final conversation = conversations[index];
        return ConversationListItem(
          conversation: conversation,
          isSelected: selectedConversationId == conversation.id,
          onTap: () {
            // 获取联系人ID（确保是字符串而非对象）
            final contact = conversation.contact;
            final contactId = contact.id;
            print('会话联系人原始数据: ${contact.runtimeType} - $contact');
            print('联系人ID类型: ${contactId.runtimeType} - $contactId');
            print('会话被点击: ${conversation.id} - ID=$contactId');
            
            // 使用ID字符串而不是整个对象
            chatProvider.selectConversation(
              conversation.id,
              contactId,
            );
            
            // 导航到聊天详情页 - 只传递ID部分
            print('准备导航到: /chat/${conversation.id}/$contactId');
            // 使用Uri编码确保特殊字符被正确处理
            final encodedUserId = Uri.encodeComponent(contactId);
            print('编码后的用户ID: $encodedUserId');
            context.push('/chat/${conversation.id}/$encodedUserId');
            print('已执行导航 (GoRouter.push)');
          },
        );
      },
    );
  }
} 